package com.example.rqweb.Okhttp;

import android.graphics.Bitmap;
import android.os.Environment;

import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.BitmapCallback;
import com.zhy.http.okhttp.callback.StringCallback;

import org.jetbrains.annotations.NotNull;

import java.io.File;
import java.io.IOException;
import java.util.concurrent.TimeUnit;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;



public class okhttpuntils
{
    OkHttpClient htp=new OkHttpClient.Builder(). connectTimeout(15, TimeUnit.SECONDS).
            readTimeout(20, TimeUnit.SECONDS).
            writeTimeout(20,  TimeUnit.SECONDS).
            retryOnConnectionFailure(true).build();


    //同步请求
    public void tb()
    {

        Request requ=new Request.Builder().get().url("http://www.baidu.com").build();
        Response res;
        {
            try {
                res = htp.newCall(requ).execute();

                //res.message().getChars();
            }
            catch (IOException e) {
                e.printStackTrace();
            }
        }


    }

    public void yb() {

        Request requ = new Request.Builder().get().url("http://www.baidu.com").build();
        Response res;
        {

            htp.newCall(requ).enqueue(new Callback() {

                @Override
                public void onFailure(@NotNull Call call, @NotNull IOException e) {

                }

                @Override
                public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException
                {

                }
            });

        }
    }
    //上传图片
    public void imag() {


        //获取sd卡中的文件
        File file = new File(Environment.getExternalStorageDirectory() + "/HeadPortrait.jpg");
        MultipartBody.Builder builder = new MultipartBody.Builder()
                //设置类型
                .setType(MultipartBody.FORM)
                //设置正文内容
                .addFormDataPart("img", "HeadPortrait.jpg",
                        RequestBody.create(MediaType.parse("image/png"), file));

        RequestBody requestBody = builder.build();

        Request request = new Request.Builder()
                .url("www.baidu.com")
                .post(requestBody)
                .build();

     /* htp.newCall(request).enqueue(new BitmapCallback() {

         @Override
         public void onError(com.squareup.okhttp.Request request, Exception e) {


         }

         @Override
         public void onResponse(Bitmap response) {

         }
      });*/
    }
    //上传文件
    public void files() {


        //获取sd卡中的文件
        File fi1 = new File(Environment.getExternalStorageDirectory() + "/HeadPortrait.jpg");
        File fi2=new File(Environment.getDataDirectory()+"/t.png");
   /*MultipartBody.Builder builder = new MultipartBody.Builder()
           //设置类型
           .setType(MultipartBody.FORM)
           //设置正文内容
           .addFormDataPart("img", "HeadPortrait.jpg",
                   RequestBody.create(MediaType.parse("image/png"), file));

   RequestBody requestBody = builder.build();


   Request request = new Request.Builder()
           .url("www.baidu.com")
           .post(requestBody)
           .build();

      /*htp.newCall(request).enqueue(new BitmapCallback() {

         @Override
         public void onError(com.squareup.okhttp.Request request, Exception e) {


         }

         @Override
         public void onResponse(Bitmap response) {

         }
      });*/


        OkHttpUtils.post().addFile("myfile","fi1",null).url("http://www.baidu.com").build().execute(new StringCallback() {
            @Override
            public void onError(com.squareup.okhttp.Request request, Exception e) {


            }

            @Override
            public void onResponse(String response) {

            }
        });


        ;

    }


}










